package org.belkodevelop.controller.json;

import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.validation.FieldError;

public class ErrorItem {

    private String fieldName;
    private String message;

    public ErrorItem(String fieldString, String message) {
        this.fieldName = fieldString;
        this.message = message;
    }

    public ErrorItem(FieldError fieldError, Locale locale, MessageSource messageSource) {
        this.fieldName = fieldError.getField();
        if (messageSource != null) {
            this.message = messageSource.getMessage(fieldError.getCode(), null, fieldError.getDefaultMessage(), locale);
        } 
        else {
            if (fieldError.getDefaultMessage() != null && !fieldError.getDefaultMessage().isEmpty()) {
                this.message = fieldError.getDefaultMessage();
            } else {
                this.message = fieldError.getCode();
            }
        }
    }

    public String getFieldName() {
        return fieldName;
    }

    public String getMessage() {
        return message;
    }
}